//
//  ForgetViewController.m
//  HomeFinder
//
//  Created by zhenguanqing on 16/2/27.
//  Copyright © 2016年 蒋永昌. All rights reserved.
//

#import "ForgetViewController.h"

#import <Masonry.h>
#import <AVOSCloud/AVOSCloud.h>

#define k_width self.view.bounds.size.width
#define k_height self.view.bounds.size.height

@interface ForgetViewController ()

@property(nonatomic,strong)UITextField *phoneTF;
@property(nonatomic,strong)UITextField *vertifyTF;
@property(nonatomic,strong)UIButton *vertifyButton;
@property(nonatomic,strong)UITextField *NewPassword;
@property(nonatomic,strong)UIButton *agreeButton;

@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"找回密码";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Comment_Icon_Back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackAction:)];
    
    
    
    //手机号码
    self.phoneTF = [[UITextField alloc]init];
    self.phoneTF.placeholder = @"手机号";
    self.phoneTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.phoneTF];
    
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(k_width * 0.8);
        make.height.mas_equalTo(k_height * 0.055);
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(k_height * 0.2);
        
    }];
    
    
    //验证码
    self.vertifyTF = [[UITextField alloc]init];
    self.vertifyTF.placeholder = @"验证码";
    self.vertifyTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.vertifyTF];
    
    [self.vertifyTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.size.equalTo(self.phoneTF);
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(k_height * 0.3);
        
        
    }];
    
    //获取验证码按钮
    self.vertifyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.vertifyButton setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    self.vertifyButton.backgroundColor = [UIColor colorWithRed:0.357 green:0.397 blue:0.389 alpha:1.000];
    self.vertifyButton.tintColor = [UIColor whiteColor];
    self.vertifyButton.titleLabel.font = [UIFont systemFontOfSize:13];

    [self.view addSubview:self.vertifyButton];
    
    [self.vertifyButton addTarget:self action:@selector(verifyAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.vertifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(k_width * 0.28, k_height * 0.035));
        make.right.equalTo(self.vertifyTF.mas_right).offset(-10);
        make.centerY.equalTo(self.vertifyTF);

        
    }];
    
    //新密码
    self.NewPassword = [[UITextField alloc]init];
    self.NewPassword.placeholder = @"新密码";
    self.NewPassword.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.NewPassword];
    
    [self.NewPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.size.equalTo(self.phoneTF);
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(k_height * 0.4);
        
        
    }];
    
    
    //确定按钮
    self.agreeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.agreeButton setTitle:@"确定" forState:UIControlStateNormal];
    self.agreeButton.backgroundColor = [UIColor colorWithRed:0.373 green:0.551 blue:1.000 alpha:1.000];
    self.agreeButton.tintColor = [UIColor whiteColor];
    [self.view addSubview:self.agreeButton];
    
    [self.agreeButton addTarget:self action:@selector(agreeAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.equalTo(self.phoneTF).multipliedBy(0.9);
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(k_height * 0.5);
        
    }];
    
    
}

-(void)goBackAction:(UIBarButtonItem*)sender{


    [self.navigationController popViewControllerAnimated:YES];

}


-(void)verifyAction:(UIButton*)sender{
    
    [AVUser requestPasswordResetWithPhoneNumber:self.phoneTF.text block:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            NSLog(@"发送验证码成功");
        } else {
            NSLog(@"发送失败:%@",error);
        }
    }];
}


-(void)agreeAction:(UIButton*)sender{
    // 验证验证码
    [AVUser resetPasswordWithSmsCode:self.vertifyTF.text newPassword:self.NewPassword.text block:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        } else {
            
            NSLog(@"找回密码失败%@",error);
        }
    }];


}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
